# -*- coding: utf-8 -*-
"""
Created on Mon Dec 07 11:33:59 2015

@author: liliangrong
"""

import urllib2
import cookielib

#声明一个CookieJar对象实例来保存cookie
cookie = cookielib.CookieJar()

#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
handler=urllib2.HTTPCookieProcessor(cookie)

#通过handler来构建opener
opener = urllib2.build_opener(handler)

#此处的open方法同urllib2的urlopen方法，也可以传入request
response = opener.open('http://www.zhihu.com/')

for item in cookie:
    #print item
    print item.name+" = "+item.value

"""
import urllib2
import cookielib

#设置保存cookie的文件，同级目录下的cookie.txt
filename = 'cookie_weibo.txt'

#声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
cookie = cookielib.MozillaCookieJar(filename)

#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
handler = urllib2.HTTPCookieProcessor(cookie)

#通过handler来构建opener
opener = urllib2.build_opener(handler)

#创建一个请求，原理同urllib2的urlopen
response = opener.open("https://www.taobao.com")

#保存cookie到文件
cookie.save(ignore_discard=True, ignore_expires=True)
"""
"""
url = "http://weibo.com/liangrong/home?wvr=5&lf=reg#1449724813817"
header = {'User-Agent':'Mozilla/5.0 (Windows NT 5.1; rv:35.0) Gecko/20100101 Firefox/35.0'}
data = {'username':'liliangrong0@126.com','password':'Llr139248'}
"""